//
//  CheatsClasses.m
//  MDC
//
//  Created by Kang on 16/8/12.
//  Copyright © 2016年 YTYangK. All rights reserved.
//

#import "CheatsClasses.h"
#import "CheatsClassesCell.h"
#import "CheatsClassesFlowLayout.h"
#import "CheatsTransition.h"

@interface CheatsClasses ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) CheatsTransition *transtion;

@end

@implementation CheatsClasses

static NSString * const reuseIdentifier = @"cheatsCell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self updateCheatsUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark  update UI
- (void)updateCheatsUI {
    self.title = @"IP设置";
    self.collectionView.backgroundColor = [UIColor clearColor];
    UIImageView *imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"美女图2.jpg"]];
    self.collectionView.backgroundView = imageView;
    self.collectionView.collectionViewLayout = [[CheatsClassesFlowLayout alloc] init];
    [self.collectionView registerClass:[CheatsClassesCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(btnAction:)];
}

- (void)btnAction:(id)btn {
    //添加连接
  //  [self.dataSource setObject:@"" atIndexedSubscript:<#(NSUInteger)#>]
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataSource.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CheatsClassesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.tag = indexPath.section;
    
      NSString * name =  [[_dataSource objectAtIndex:indexPath.section] objectForKey:@"CheatsName"];
    NSString * ip       = [[_dataSource objectAtIndex:indexPath.section] objectForKey:@"CheatsIP"];
    cell.tiletLabel.text  = [NSString stringWithFormat:@"%@:%@",name,ip];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSLog(@"----->%@",[_dataSource objectAtIndex:indexPath.section]);
    [[NSUserDefaults standardUserDefaults] setObject:[[_dataSource objectAtIndex:indexPath.section] objectForKey:@"CheatsIP"] forKey:@"CheatsVlue"];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    //TODO:显示内容，暂时不做
//    CheatsClassesCell * selectedCell = (CheatsClassesCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    NSArray * visibleCells = collectionView.visibleCells;
//    UIStoryboard * stb  = [UIStoryboard storyboardWithName:@"Cheats" bundle:[NSBundle mainBundle]];
//    EvernoteDetailController * VC = [stb instantiateViewControllerWithIdentifier:@"EvernoteDetailController"];
//    CGRect finalFrame = CGRectMake(10, collectionView.contentOffset.y + 30,  CHEATS_SCREEN_W- 20, CHEATS_SCREEN_H - 40);
//    [self.transtion cheatsTransitionWithSelectCell:selectedCell visibleCells:visibleCells originFrame:selectedCell.frame finalFrame:finalFrame panViewController:VC listViewController:self];
//    VC.transitioningDelegate = self.transtion;
//    VC.delegate = self.transtion;
//    [self presentViewController:VC animated:YES completion:^{
//        
//    }];
}


/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/



#pragma mark - getter 
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
        NSString *cheatsDataList = [[NSBundle mainBundle] pathForResource:@"CheatsList" ofType:@"plist"];
        NSMutableArray *list = [[NSMutableArray alloc] initWithContentsOfFile:cheatsDataList];
//        for (NSString *ip in list) {
//            [_dataSource addObject:[NSString stringWithFormat:@"%@",ip]];
//        }
        _dataSource = list;
    }
    return _dataSource;
}

- (CheatsTransition *)transtion {
    if (!_transtion) {
        _transtion = [[CheatsTransition alloc] init];
    }
    return _transtion;
}

//- (void)setCheatsNav:(UINavigationController *)cheatsNav {
//    if (cheatsNav != nil) {
//    }
//}

@end
